package com.dtt.company.employee.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.dtt.company.department.model.DepartmentModel;
import com.dtt.company.employee.model.EmployeeModel;
import com.dtt.company.employee.service.EmployeeService;
import com.dtt.company.util.FmtRequest;
import com.dtt.company.util.MD5;
import com.dtt.company.util.UploadUtil;
@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	//接收请求-获取参数-封装对象-调用方法（传递数据）-返回结果（页面跳转）
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override 
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		req.setCharacterEncoding("UTF-8");
//		resp.setCharacterEncoding("UTF-8");
		String action=req.getParameter("action");
		String result="";
		switch(action) {
			case "reg":
			    result= reg(req);
				break;
			case "login":
				result=login(req);
				break;
			case "add":
			    result= add(req);
				break;
			case "sel":
				result=sel(req);
				break;
			case "del":
				result=del(req);
				break;
			case "get":
				result=get(req);
				break;
			case "upd":
				result=upd(req);
				break;
			case "list":
				result=list(req);
				break;
			case "resetPsd":
				result=resetPsd(req);
				break;
			case "updpass":
				result=updpass(req);
				break;
			case "upddept":
				result=upddept(req);
				break;
			case "logout":
				req.getSession().removeAttribute("user");
				resp.sendRedirect(req.getContextPath()+"/web/login.jsp");
				return;
			case "delpic"://删除图片
				result=delpic(req);
				break;
			case "refhead"://更新session里的头像
				refhead(req);
				resp.sendRedirect(req.getContextPath()+"/web/page/main.jsp");
				break;	
			default:
				break;
		}
				
//		PrintWriter out=resp.getWriter();
//		out.append(result);
//		out.flush();
//		out.close();
//		out=null;
		FmtRequest.out(resp, result);
	}  
	

	private void refhead(HttpServletRequest req) {
		String code=req.getParameter("code");
		EmployeeModel model=new EmployeeModel();
		model.setCode(code);
		EmployeeModel mdb=service.selectModel(model);//用账号去查
		req.getSession().setAttribute("user", mdb);
		System.out.println(req.getSession().getAttribute("user"));
		return;
	}
	private String delpic(HttpServletRequest req) {
		String code=req.getParameter("code");
		EmployeeModel model=new EmployeeModel();
		model.setCode(code);
		EmployeeModel dbm=service.selectModel(model);
		String image=dbm.getHead();
		UploadUtil.delFile(image);//删除服务器端图片
		model.setHead("");
		return service.update(model);//删除数据库中的头像
		}
	private String upddept(HttpServletRequest req) {
		String code=req.getParameter("code");
		String depCode=req.getParameter("depCode");
		EmployeeModel model=new EmployeeModel();
		model.setCode(code);
		model.setDepCode(depCode);
		return service.update(model);
		}

	private String updpass(HttpServletRequest req) {
		String code=req.getParameter("code");
		String psd=req.getParameter("psd");
		psd=MD5.encode(psd);
		EmployeeModel model=new EmployeeModel(code,psd);
		return service.update(model);
		}

//	private void out(HttpServletResponse resp,String result) throws IOException {
//		PrintWriter out=resp.getWriter();
//		out.append(result);
//		out.flush();
//		out.close();
//		out=null;
//	}
	
	private String resetPsd(HttpServletRequest req) {
		String code=req.getParameter("code");
		EmployeeModel model=new EmployeeModel(code,getDefaultPass());
		System.out.println(model.getPsd());
		return service.update(model);
	}

	private String getDefaultPass() {
		return MD5.encode("123456");
}

	EmployeeService service=new EmployeeService();
	
	private String login(HttpServletRequest req) {
		String yzm=req.getParameter("yzm");
		Object yzmSession=req.getSession().getAttribute("verifycode");
		if(!yzmSession.toString().equals(yzm))
			return "F";
		
		String code=req.getParameter("code");
		String psd=req.getParameter("psd");
		psd=MD5.encode(psd);
		EmployeeModel model=new EmployeeModel(code,psd);
		EmployeeModel mdb=service.selectModel(model);//用账号去查，可得出账号是否存在，再进一步可判断密码是否正确
		if(mdb==null)
			return "0";
		if(!mdb.getPsd().equals(psd))
			return "2";
		req.getSession().setAttribute("user", mdb);
//		EmployeeModel mo=(EmployeeModel)req.getSession().getAttribute("user");
//		System.out.println(mo.getCode());
		return "1";
		
//		return service.login(model);
	}

	private String reg(HttpServletRequest req) {
		String code=req.getParameter("code");
		String name=req.getParameter("name");
		String psd=req.getParameter("psd");
//		System.out.println(code+"\t"+name+"\t"+psd);
		String yzm=req.getParameter("yzm");
//		System.out.println("yzm=="+yzm);
		Object yzmSession=req.getSession().getAttribute("verifycode");
		if(!yzmSession.toString().equals(yzm)) 
			return "F";//验证码错误
	
		psd=MD5.encode(psd);
		EmployeeModel model=new EmployeeModel(code,psd,name);
		
		return service.insert(model);
	}
	
	//分页
		private String list(HttpServletRequest req) {
			String code=req.getParameter("code");
			String name=req.getParameter("name");
			String pageIndex=req.getParameter("pageIndex");
			String pageLimit=req.getParameter("pageLimit");
			EmployeeModel model=new EmployeeModel();
			model.setCode(code);
			model.setName(name);
			model.setPageIndex(Integer.parseInt(pageIndex));//将页面传来的转换为int类型
			model.setPageLimit(Integer.parseInt(pageLimit));
			model.setPageOn(true);
			
			List<EmployeeModel> list = service.select(model);
//			return new JSONArray(list).toString();
			int count=service.selectCount(model);
			Map<String,Object> map=new HashMap<>();
			map.put("data", list);
			map.put("count",count);
			return  new JSONObject(map).toString();
			
		}
		private String upd(HttpServletRequest req) {
			String code=req.getParameter("code");
			String name=req.getParameter("name");
			String depCode=req.getParameter("depCode");
			EmployeeModel model=new EmployeeModel();
			model.setCode(code);
			model.setName(name);
			model.setDepCode(depCode);
			return service.update(model);
		}
		private String get(HttpServletRequest req) {
			String code=req.getParameter("code");
			EmployeeModel model = new EmployeeModel();
			model.setCode(code);
			EmployeeModel mdb = service.selectModel(model);
//			System.out.println("data====="+mdb.toString());
			return new JSONObject(mdb).toString();
		}
		private String del(HttpServletRequest req) {
			String code=req.getParameter("code");
			EmployeeModel model = new EmployeeModel();
			model.setCode(code);
			return service.delete(model);
		}
		private String sel(HttpServletRequest req) {
			String code=req.getParameter("code");
			String name=req.getParameter("name");
			EmployeeModel model=new EmployeeModel();
			model.setCode(code);
			model.setName(name);
			List<EmployeeModel> list = service.select(model);
			return new JSONArray(list).toString();//json 数组格式的字符串
		}
		
		private String add(HttpServletRequest req) {
			String code=req.getParameter("code");
			String name=req.getParameter("name");
			String depCode=req.getParameter("depCode");
			EmployeeModel model=new EmployeeModel();
			model.setCode(code);
			model.setName(name);
			model.setDepCode(depCode);
			String psd=getDefaultPass();
			model.setPsd(psd);			
			String result=service.insert(model);
			System.out.println(result);
			return result;
		}

}
